package cn.itcast.contorller.user;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.Dept;
import cn.itcast.domain.Role;
import cn.itcast.domain.User;
import cn.itcast.service.dept.DeptService;
import cn.itcast.service.role.RoleService;
import cn.itcast.service.user.UserService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@RequestMapping(value = "/system/user")
@Controller
public class UserController extends BaseContorller {
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;
    @RequestMapping(value = "/list",name = "pageHelper插件分页查询用户")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
       PageInfo pageInfo= userService.findAllUser(pageNum,pageSize,company_id);
       request.setAttribute("pb",pageInfo);
       return "system/user/user-list";
    }
    @RequestMapping(name = "到新建用户页面",value = "/toAdd")
    public String toAdd(){
        List<Dept> allDept = deptService.findAllDept(company_id);
        request.setAttribute("deptList",allDept);
        return "system/user/user-add";
    }
    @RequestMapping(value = "/edit",name = "新增/修改")
    public String edit(User user){
        //指定当前用户是属于哪个公司的
        user.setCompanyId(company_id);
        user.setCompanyName(company_name);
        /*添加用户的方法*/
        if (UtilFuns.isEmpty(user.getId())){
            /*shiroMd5加密  密码*/
            Md5Hash md5Hash = new Md5Hash(user.getPassword(),user.getEmail(),1);
            user.setPassword(md5Hash.toString());
            user.setId(UUID.randomUUID().toString());
            userService.save(user);
        }else {
            /*如果用户修改了密码  就 把密码再次的加密 然后保存到数据库*/
            User loginUser =(User) session.getAttribute("loginUser");
            if (!user.getPassword().equals(loginUser.getPassword())){
                Md5Hash md5Hash = new Md5Hash(user.getPassword(),user.getEmail(),1);
                user.setPassword(md5Hash.toString());
            }
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "到修改用户页面回显数据")
    public String toUpdate(String id){
      User user=  userService.findById(id);
      request.setAttribute("user",user);
        List<Dept> allDept = deptService.findAllDept(company_id);
        request.setAttribute("deptList",allDept);
        return "system/user/user-update";
    }
    @RequestMapping(value = "/delete.do",name = "删除员工")
    public String delete(String id){
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }
    @RequestMapping(value = "/roleList",name = "到用户分配角色页面")
    public String roleList(String id){
        /*查询当前的用户*/
        User user = userService.findById(id);
        request.setAttribute("user",user);
        /*查询所有的角色*/
       List<Role> list= roleService.findRole(company_id);
       request.setAttribute("roleList",list);
       /*查询当前用户所对应得角色*/
        List<Role> roleList= roleService.findUserRole(id);
        String str="";
        for (Role role : roleList){
            str+=role.getId()+",";
        }
        System.out.println("查询当前用户所对应得角色"+roleList);
        request.setAttribute("userRoleStr",str);//把角色id 拼接成字符  返回前台做对比
        return "system/user/user-role";
    }
    @RequestMapping(value = "/changeRole",name = "保存当前用户的角色")
    public String changeRole(String userid,String[] roleIds){
        userService.saveUserRole(userid,roleIds);
        return "redirect:/system/user/list.do";
    }

    }
